package com.asset.dispoal.interestFormula.domain.service.commom;

import com.asset.dispoal.commom.page.PagingQuery;
import com.asset.dispoal.interestFormula.domain.db.entity.InterestFormula;
import com.asset.dispoal.interestFormula.domain.db.mapper.InterestFormulaMapper;
import com.asset.dispoal.interestFormula.domain.service.converter.InterestFormulaConverter;
import com.asset.dispoal.interestFormula.dto.InterestFormulaRequest;
import com.asset.dispoal.interestFormula.dto.InterestFormulaResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;
import tech.mhuang.pacebox.springboot.protocol.Result;
import tech.mhuang.pacebox.springboot.protocol.data.Page;
import tech.mhuang.pacebox.springboot.protocol.data.PageVO;

import java.util.List;

/**
 * @author qr
 * @date 2022/3/28$
 */
@Component
public class InterestFormulaQueryPageCommand implements BaseCommand<InterestFormulaRequest, Result> {

    @Autowired
    InterestFormulaMapper interestFormulaMapper;

    @Override
    public Result<PageVO<InterestFormulaResponse>> execute(InterestFormulaRequest request) {
        InterestFormula interestFormula = InterestFormulaConverter.INSTANCE.reqToDo(request);
        interestFormula.setPermission((String) GlobalHeaderThreadLocal.getOrException().getExtraMap().get("permission"));
        int i = interestFormulaMapper.queryCount(interestFormula);
        Page<InterestFormula> page = PagingQuery.getPage(i, interestFormula);
        PageVO<InterestFormulaResponse> pageVO = new PageVO<>();
        pageVO.setTotalSize(i);
        if (page != null) {
            List<InterestFormulaResponse> interestFormulas = interestFormulaMapper.queryPage(page);
            pageVO.setResult(interestFormulas);
        }
        return Result.ok(pageVO);
    }
}
